<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE HTML>  
<html>  
<head>  
<meta http-equiv="content-type" content="text/html;charset=utf-8">  
<script type="text/javascript" >  
      
    // 向HTML追加message，这个函数是给服务器向iframe中添加的javascript脚本调用  
    function addMsg(msg) {  
        var msgElement = document.getElementById("msg");  
        msgElement.innerHTML += msg;  
    }  
      
    // 点击“发送”按钮后Ajax发送消息  
    function sendMsg() {  
        var xmlhttp = new XMLHttpRequest();  
        xmlhttp.open("POST", "AjaxMessageServlet"); // sendMsg是AjaxMessageServlet对应的URL  
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
          
        var name = document.getElementById("input-name").value;  
        var msg = document.getElementById("input-msg").value;  
        xmlhttp.send("name=" + encodeURIComponent(name) + "&msg=" + encodeURIComponent(msg));  
        document.getElementById("input-msg").value = "";  
    }  
    
  //服务器timeout后再重新加载iframe  
	function iframeRefresh() {  
	    var iframeElement = document.getElementById("iframe");  
	    iframeElement.src = iframeElement.src;  
	    
	} 
      
    
      
</script>  
</head>  
<body>  
      
    <!-- 隐藏的iframe，src="comet"是CometServlet对应的URL，用于加载服务器推送的JS脚本 -->  
    <iframe id="iframe" style="display: none;" src="CometServlet" onload="iframeRefresh();"></iframe>  
      
    <div id="msg" style="height: 400px; width: 400px; overflow: scroll;"></div>  
      
    姓名：<input type="text" id="input-name"><br>  
    消息：<br>  
    <textarea id="input-msg" cols="30" rows="4"></textarea><br>  
    <input type="button" value="发送" onclick="sendMsg();">  
  
</body>  
</html> 